﻿ <!--
  Odatavalidator ver. 1.0
  Copyright (c) Microsoft Corporation
  All rights reserved.

  Licensed under the Apache License, Version 2.0 (the ""License"");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

  THIS CODE IS PROVIDED ON AN  *AS IS* BASIS, WITHOUT WARRANTIES OR
  CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
  LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS
  FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.

  See the Apache Version 2.0 License for specific language governing
  permissions and limitations under the License.
-->

<rules>
  <rule id="Entry.Core.4090" category="core" target="entry" v4specificationsection ="7.3" requirementlevel="must" metadata="true" format="JsonLight" version="v3_v4">
    <description>Each element in the JSON array is the representation of a primitive value.</description>
    <action processor="xslt+jsonschema">
      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="text"/>
        <xsl:template match="/">
          {
          "additionalProperties" : true,
          "type" : "object",
          "properties" : {
          <!-- Get all the Properties and NavigationProperties -->
          <xsl:apply-templates select="*[local-name()='Edmx']/*[local-name()='DataServices']/*[local-name()='Schema']/*[local-name()='EntityType'][@Name='$ENTITYTYPE$']"/>
          }
          }
        </xsl:template>

        <xsl:template match="*[local-name()='Edmx']/*[local-name()='DataServices']/*[local-name()='Schema']/*[local-name()='EntityType'][@Name='$ENTITYTYPE$']">
          <xsl:for-each select="*[local-name()='Property']">
            <!-- select type of property -->
            <xsl:variable name="type">
              <xsl:value-of select="@Type"/>
            </xsl:variable>
            <xsl:if test="starts-with($type, 'Collection(Edm.')">
              <xsl:variable name="primitiveType">
                <xsl:value-of select="substring-before(substring-after($type, 'Collection('), ')')"/>
              </xsl:variable>
              <!-- select complex type property-->
              "<xsl:value-of select="@Name"/>" : {
              "type" : "array",
              "items" : {
              <xsl:choose>
                <xsl:when test="$primitiveType='Edm.Boolean'">
                  "type" : ["boolean", "null"],
                </xsl:when>
                <!-- Geography and geometry and their sub types values are object type-->
                <xsl:when test="starts-with($primitiveType, 'Edm.Geography') or starts-with($primitiveType, 'Edm.Geometry')">
                  "type" : ["object", "null"],
                </xsl:when>
                <xsl:when test="$primitiveType='Edm.Byte' or $primitiveType='Edm.SByte' or $primitiveType='Edm.Int16' or $primitiveType='Edm.Int32'">
                  "type" : ["integer", "null"],
                </xsl:when>
                <xsl:when test="$primitiveType='Edm.Single' or $primitiveType='Edm.Double'">
                  "type" : ["number", "null"],
                </xsl:when>
                <!-- Int64  can be integer or string-->
                <xsl:when test="$primitiveType='Edm.Int64'">
                  "type" : ["integer", "string", "null"],
                </xsl:when>
                <!-- Decimal  can be number or string-->
                <xsl:when test="$primitiveType='Edm.Decimal'">
                  "type" : ["number", "string", "null"],
                </xsl:when>
                <xsl:otherwise>
                  "type" : ["string", "null"],
                </xsl:otherwise>
              </xsl:choose>
              }},
            </xsl:if>
          </xsl:for-each>
        </xsl:template>
      </xsl:stylesheet>
    </action>
    <errormessage>Each element in the JSON array is the representation of a primitive value.</errormessage>
  </rule>
</rules>